package com.whl.sm.service.impl.expense;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.expense.MaterBillMapper;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.MaterBill;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.MaterBillService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.notice.CommunityNoticeService;
import com.whl.sm.service.system.OrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-18
 */
@Service
public class MaterBillServiceImpl extends ServiceImpl<MaterBillMapper, MaterBill> implements MaterBillService {

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private ComUnitsService comUnitsService;

    @Autowired
    private AssetsOwnerService assetsOwnerService;

    @Autowired
    private AssetsTenantService assetsTenantService;

    @Autowired
    private CommunityNoticeService communityNoticeService;

    @Override
    public Result update(MaterBill materBill) {
        materBill.setOperator(orgUserService.getCurrentUserId());
        if (materBill.getId() == null){
            getBaseMapper().insert(materBill);
        }else {
            getBaseMapper().updateById(materBill);
        }
        return Result.ok();
    }

    @Override
    public Result payBill(String billId) {
        String userId = orgUserService.getCurrentUserId();
        MaterBill materBill = getBaseMapper().selectById(billId);
        materBill.setPaymentStatus(1);
        materBill.setPayer(userId);
        materBill.setOperator(userId);
        materBill.setPaymentTime(new Date());
        try {
            getBaseMapper().updateById(materBill);
        }catch (Exception e){
            log.error("账单支付失败=======》"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public  Result sendMessage(List<String> idList) {
        try {
            for (String materBillId : idList){
                MaterBill bill = getBaseMapper().selectById(materBillId);
                ComUnits linkUnit = comUnitsService.getById(bill.getUnitId());
                List<String>  linkUsers = new ArrayList<>();
                if (linkUnit.getUnitStatus() == 1){
                    //1:为售出
                    List<AssetsOwner> linkOwner = assetsOwnerService.list(new QueryWrapper<AssetsOwner>()
                            .eq("assets_id",linkUnit.getId())
                            .ne("is_del",1)
                    );
                    linkUsers = linkOwner.stream().map(AssetsOwner::getUserId).collect(Collectors.toList());
                }else if(linkUnit.getUnitStatus() == 2){
                    //2为租赁
                    List<AssetsTenant> linkTenancy = assetsTenantService.list(new QueryWrapper<AssetsTenant>()
                            .eq("assets_id",linkUnit.getId())
                            .ne("is_del",1)
                    );
                    linkUsers = linkTenancy.stream().map(AssetsTenant::getUserId).collect(Collectors.toList());
                }
                String title = "水电账单催缴";
                String noticeContent = "通知：<br>" +
                        "你好，所属居住单元("+linkUnit.getUnitCode()+")"
                        +"存在尚未缴纳水电账单，为不影响所属单元正常用水、用电，请及时缴费为谢！<br>如给您带来不便，敬请谅解！";
                communityNoticeService.buildPersonalNotice(linkUsers,title,noticeContent);
            }

            return Result.ok();
        }catch (Exception e){
            log.error("账单催发信息发送失败----->"+e.getMessage());
            return Result.fail();
        }
    }
}
